package com.student.recruitment.app.controller;

import java.security.Principal;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.student.recruitment.app.entity.EmployeeEntity;
import com.student.recruitment.app.service.EmployeeManager;

@Controller
public class LoginController {
 
	@Autowired
    private EmployeeManager employeeManager;
	
	@RequestMapping(value =  "/welcome**", method = RequestMethod.GET)
	public String welcomePage(@ModelAttribute EmployeeEntity employee, BindingResult result,Principal principal, ModelMap map) {
 
    	//map.addAttribute("employee", new EmployeeEntity());

		String username = principal.getName();
		String status = employeeManager.getEmployeeStatus(username);
		
		int punkty = employeeManager.getEmployeePoints(username);
		map.addAttribute("employee", employeeManager.getEmployeeData(username));
		
		map.addAttribute("status", status);
		map.addAttribute("punkty", punkty);
		
		return "hello";
 
	}
 
	
	@RequestMapping(value = { "/"}, method = RequestMethod.GET)
	public ModelAndView startPage() {
 
		ModelAndView model = new ModelAndView();
		
		model.setViewName("logins");
 
		return model;
 
	}
	
	@RequestMapping(value = {"/admins" }, method = RequestMethod.GET)
	public ModelAndView adminPage() {
 
		ModelAndView model = new ModelAndView();
		model.addObject("title", "securitys page");
		model.addObject("message", "This iss security page!");
		//model.addObject("has", "ss");
		model.setViewName("admins");
 
		return model;
 
	}
 
	//Spring Security see this :
	@RequestMapping(value = "/logins", method = RequestMethod.GET)
	public ModelAndView login(
		@RequestParam(value = "error", required = false) String error,
		@RequestParam(value = "logout", required = false) String logout) {
		
		ModelAndView model = new ModelAndView();
		if (error != null) {
			model.addObject("error", "Nieprawidlowy login i/lub haslo");
		}
 
		if (logout != null) {
			model.addObject("msg", "Jestes wylogowany");
		}
		model.setViewName("logins");
 
		return model;
 
	}
 
}